/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lmclogistics;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

/**
 *
 * @author Sonia Tan
 */
public class LoadingScreen extends JFrame{
    private int duration;
    private Image background;

    public LoadingScreen(int d) {
        duration = d;
        showSplash();
    }

    // A simple little method to show a title screen in the center
    // of the screen for the amount of time given in the constructor
    public void showSplash() {

//        Image image;
        updateBackground();
//        setAlwaysOnTop(true);
        setUndecorated(true);
//        setIconImage(image);
//        setBackground(null);
//        setForeground(null);
        setVisible(true);
 

        int width = 600 ;
        int height = 350;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width-width)/2;
        int y = (screen.height-height)/2;
        setBounds(x,y,width,height);

        // Build the splash screen
        JLabel label = new JLabel();
        label.setIcon(new ImageIcon(getClass().getResource("/images/loading.png")));

        add(label, BorderLayout.CENTER);




        // Display it
        setVisible(true);

        // Wait a little while, maybe while loading resources
        try { Thread.sleep(duration); } catch (Exception e) {}

        setVisible(false);

    }

    public void showSplashAndExit() {

        showSplash();
        dispose();


    }
    public void updateBackground() {
    try {
        Robot rbt = new Robot( );
        Toolkit tk = Toolkit.getDefaultToolkit( );
        Dimension dim = tk.getScreenSize( );
        background = rbt.createScreenCapture(
        new Rectangle(0,0,(int)dim.getWidth( ),
                          (int)dim.getHeight( )));
    } catch (Exception ex) {

        ex.printStackTrace( );
    }
}

    public void paintComponent(Graphics g) {
        Point pos = this.getLocationOnScreen( );
        Point offset = new Point(-pos.x,-pos.y);
        g.drawImage(background,offset.x,offset.y,null);
        }

}
